using System;
using System.Linq;
using Db4objects.Db4o;
using Db4objects.Db4o.Linq;

namespace SharedObjects
{
	public abstract class MissionObjective : EntityBase
	{
		protected abstract bool IsObjectiveComplete(IObjectContainer db, params ulong[] parameters);
	}

	//Steal (credential) file and place it on completion cs
	public class AcquireFile : MissionObjective
	{
		public ulong SourceFileId { get; set; } //if a file must be acquired, we check if the source file actually exists (delete status irrelevant)
		
		/// <summary>
		/// 
		/// </summary>
		/// <param name="db"></param>
		/// <param name="parameters">0 = file, 1 = system</param>
		/// <returns></returns>
		protected override bool IsObjectiveComplete(IObjectContainer db, params ulong[] parameters)
		{
			SystemFile file = (from ComputerSystem cs in db where cs.ID == parameters[1] from SystemFile f in db where f.ID == parameters[0] && f.ParentId == cs.RootDirectory.ID select f).FirstOrDefault();
			return file != null;
		}
	}

	public class DeleteFile : MissionObjective
	{
		public ulong SourceFileId { get; set; } //if a file must be deleted, we check if a SystemFile exists with isDeleted == true && id == SourceFileId

		/// <summary>
		/// Checks the objective
		/// </summary>
		/// <param name="db"></param>
		/// <param name="parameters">0 = file id, 1 = system id</param>
		/// <returns></returns>
		protected override bool IsObjectiveComplete(IObjectContainer db, params ulong[] parameters)
		{
			SystemFile file = (from SystemFile f in db where f.ID == parameters[0] && f.IsDeleted == true select f).FirstOrDefault();
			if(file == null)
				return false;
			SystemDirectory root = (from SystemDirectory d in db where d.ID == file.ParentId select d).FirstOrDefault();
			if(root == null)
				return false;
			ComputerSystem system = (from ComputerSystem cs in db where cs.RootDirectory.ID == root.ID && cs.ID == parameters[1] select cs).FirstOrDefault();
			if (system == null)
				return false;
			return true;
		}
	}

	public class DeleteAllFiles : MissionObjective
	{
		public ulong SystemId { get; set; }
		protected override bool IsObjectiveComplete(IObjectContainer db, params ulong[] parameters)
		{
			throw new NotImplementedException();
		}
	}

	public class SabotageSystem : MissionObjective
	{
		public ulong SystemId { get; set; }
		protected override bool IsObjectiveComplete(IObjectContainer db, params ulong[] parameters)
		{
			throw new NotImplementedException();
		}
	}

	public class ChangeRecord : MissionObjective
	{
		public ulong RecordId { get; set; }
		public ulong SystemId { get; set; } //the system the record exists on		
		protected override bool IsObjectiveComplete(IObjectContainer db, params ulong[] parameters)
		{
			throw new NotImplementedException();
		}
	}
}